var Md5 = require('../utils/md5.js');
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const formatdate = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    return [year, month].map(formatNumber).join('-')
};

const formatDate1 = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()

	return [year, month, day].map(formatNumber).join('-')
}
function http(url, data, callback) {
    data.key = getApp().globalData !== undefined ? getApp().globalData.key : '';

	// data.key ="e6ca7072221e6538d3a0f3c2ff65964e"
    // data.key = wx.getStorageSync('key') !== undefined ? wx.getStorageSync('key') : '';
    data.timestamp = Date.parse(new Date()) / 1000;
    var sign = Md5.hexMD5(data.timestamp + 'iqidian' + data.key); //md5加密
    data.sign = sign;
    if (data.goods_list !== undefined) {
        data.goods_list = JSON.stringify(data.goods_list);
    }
    wx.request({
        url: getApp().globalData.http + url,
        data: data,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        dataType: "json",
        success: function (res) {
            callback(res.data)
        },
        fail: function (res) { },
        complete: function (res) { },
    })
};

function message(msg) {
    wx.showModal({
        title: '温馨提示',
        content: msg,
        showCancel: false,
        success(res) { }
    })
}
function getJson(url) {
	var arr = url.split('?')[1].split('&')
	var theRequest = new Object();
	for (var i = 0; i < arr.length; i++) {
		var kye = arr[i].split("=")[0]
		var value = arr[i].split("=")[1]
		// 给对象赋值
		theRequest[kye] = value
	}
	return theRequest;
}
function confirm(msg, success, fail) {
	wx.showModal({
		title: '温馨提示',
		content: msg,
		showCancel: true,
		confirmColor: "#59A5F0",
		cancelColor: "#99a0a2",
		success: success,
		fail: fail
	})
}

function showToast(msg) {
    wx.showToast({
        title: msg,
        icon: "none"
    })
}

function showLoading(msg) {
    wx.showLoading({
        mask: true,
        title: msg ? msg : '加载中',
    })
}
function showSuccess(msg, icon, mask, time) {
	icon = icon ? icon.toString() : 'success';
	mask = mask === false || mask === true ? mask : true;
	wx.showToast({
		title: msg ? msg : '成功',
		icon: icon,
		mask: mask,
		duration: time ? time : 2000,
	});
}
function showTishi(msg, icon, mask, time) {
	icon = icon ? icon.toString() : 'none';
	mask = mask === false || mask === true ? mask : true;
	wx.showToast({
		title: msg ? msg : '操作成功',
		icon: icon,
		mask: mask,
		duration: time ? time : 800,
	});
}
function hideLoading() {
    wx.hideLoading();
}
function countdown(that) {
    var EndTime = that.data.end_time || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    console.log('剩余时间：' + total_micro_second);
    // 渲染倒计时时钟
    that.setData({
        clock: dateformat(total_micro_second)
    });
    if (total_micro_second <= 0) {
        that.setData({
            clock: "已经截止"
        });
        //return;
    }
    setTimeout(function () {
        total_micro_second -= 1000;
        countdown(that);
    }, 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
}
// getPastTime:判断发布过去时间间隔
function getPastTime(time) {
    // time为传入的时间戳        
    let date = new Date();
    let time1 = date.getTime();
    let time2 = (time1 - time) / 1000;
    let pastTimes = '';
    if (time2 < 300) {
        pastTimes = '刚刚';
    } else if (time2 >= 300 && time2 < 1800) {
        pastTimes = '5分钟前';
    } else if (time2 >= 1800 && time2 < 3600) {
        pastTimes = '30分钟前';
    } else if (time2 >= 3600 && time2 < 86400) {
        pastTimes = '1小时前';
    } else if (time2 >= 86400 && time2 < 259200) {
        pastTimes = '一天前';
    } else if (time2 >= 259200) {
        let date3 = new Date(time);
        pastTimes = date3.getFullYear() + '-' + (date3.getMonth() + 1) + '-' + date3.getDate();
    }
    return pastTimes;
}

module.exports = {
    formatTime: formatTime,
    formatdate: formatdate,
	formatDate1: formatDate1,
    showLoading: showLoading,
    hideLoading: hideLoading,
    message: message,
    http: http,
    getPastTime: getPastTime,
    showToast: showToast,
	confirm: confirm,
	showTishi: showTishi,
	showSuccess: showSuccess,
	getJson: getJson
}